package com.shanyu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.shanyu.dto.ResponseDto;
import com.shanyu.dto.WxTokenDto;
import com.shanyu.dto.WxUserDto;
import com.shanyu.eo.MemberEo;
import com.shanyu.service.IMemberService;
import com.shanyu.service.IWxService;
import com.shanyu.utils.IdGen;

@RestController
@RequestMapping(value = "/member/auth")
public class WxController {
	
	@Autowired
	private IWxService wxService;
	
	@Autowired
	private IMemberService memberService;
	
	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public ResponseDto login(String code){
		WxTokenDto token = wxService.getUserInfoAccessToken(code);
		WxUserDto userDto = wxService.getUserInfo(token.getSessionKey(), token.getOpenId());
		
		MemberEo eo = memberService.selectMemberByOpenId(token.getOpenId());
		if(eo == null){
			eo = new MemberEo();
			eo.setId(IdGen.creatUUID());
			eo.setImgUrl(userDto.getHeadImgUrl());
			eo.setNickName(userDto.getNickname());
			eo.setOpenId(userDto.getOpenId());
			memberService.insert(eo);
		}
		return new ResponseDto(200, eo);
	}

}
